package com.baihan.angus.app.game.busshow.adps;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.base.BaseAdp;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.common.bean.response.PropResponse;

public class PropsAdp extends BaseAdp<PropResponse.DataX.Data, PropsAdp.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    @Override
    protected RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_prop, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void bindYourViewHolder(@NonNull ViewHolder holder, int position) {
        ViewHolder vh = holder;

        PropResponse.DataX.Data propData = getItem(position);

        vh.tvTitle.setText(propData.getTitle());
        vh.tvContent.setText(propData.getContent());
        if (propData.getIs_use() == 0) {//0-未使用 1-已使用
            vh.tvUsed.setText("未使用");
            vh.tvUsed.setBackgroundResource(R.mipmap.ic_btn_green);
        } else if (propData.getIs_use() == 1) {
            vh.tvUsed.setText("已使用");
            vh.tvUsed.setBackgroundResource(R.mipmap.ic_btn_red);
        }

        vh.flItem.setOnClickListener(view -> {
            mOnItemClickListener.onItemClick(position);
        });
    }

    @Override
    protected int getEmptyView() {
        return 0;
    }

    public void removeItem(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, mData.size() - position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvTitle, tvUsed, tvContent;
        ImageView ivRedPoint;
        FrameLayout flItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvUsed = itemView.findViewById(R.id.tvUsed);
            tvContent = itemView.findViewById(R.id.tvContent);
            ivRedPoint = itemView.findViewById(R.id.ivRedPoint);
            flItem = itemView.findViewById(R.id.flItem);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}
